/**
 * Enemy
 * Template of enemy entity
 **/
var Enemy = function() {

// CONFIGURATION ---
	this.conf = null;

	this.sprite = undefined;
	this.sprites = [];

	this.state = 'move';
	this.animateHitCount = null;

	this.hitDate = +new Date();

	this.toGarbage = false;
// --- END CONFIGURATION

// CORE ---
	this.init = function(typeOf) {
		log('*** INIT ENEMY : ' + typeOf + '***');
		this.conf = EnemyConf[typeOf];

		var conf = this.conf.sprites;
		for(cName in conf) {
			// Save sprite instance and animation linked
			this.sprites[cName] = conf[cName].img;
		}

		this.w = this.conf.bounds.w;
		this.h = this.conf.bounds.h;

		// Place enemy outside the canvas
		this.x = WIDTH + this.w; 
		this.y = game.mLevel.level.getGround() - this.h;

		this.life = this.conf.life;
	}

	this.update = function() {
		// Play move animation
		if (this.state === 'move' || game.currentPlayer() === undefined) {
			this.doMove();
		}
		else {
			var intervalHit = interval(this.hitDate, this.conf.hitSpeed);
			if(intervalHit === true) {
				this.doHit();
			} else {
				this.state = 'idle';
			}
		}

		if(this.life <= 0) {
			this.doDie();
		}

		this.animate();
	}

	this.animate = function() {

		// Default animation (idle)
		var sprite = 'idle';

		switch(this.state) {
			// Run animation for moving
			case 'move' :
				sprite = 'walk';
			break;
			// Hit animation 
			case 'hit' :
				sprite = 'hit';
				break;
			// Default animation (idle)
			default : 
				sprite = 'idle';
			break;
		}

		if(typeof this.sprite !== "undefined" && this.sprite.name !== sprite) {
			this.sprite = IM.killInstance(this.sprite);
		} 
		
		if(typeof this.sprite === "undefined") {
			this.sprite = IM.getInstance(getIMName(this.sprites[sprite]));
			this.sprite.animation = new IIG.Animation(this.conf.sprites[sprite].animation);
			this.sprite.name = sprite;
		}
	}

	this.render = function() {
		// Render sprites in the appropriate direction
		IM.drawImage(CTX, this.sprite, this.x, this.y);

		if(this.conf.debug === true) {
			this.debug();
		}
	}
// --- END CORE

// ACTIONS --- 
	this.doMove = function() {
		var x = (game.currentPlayer() !== undefined) 
			? game.currentPlayer().getX() + PlayerConf.bounds.w 
			: - this.w;

		if(this.x + this.conf.hitOffset > x) {
			this.x -= this.conf.speed;
		} else {
			this.state = 'idle';
		}
	}

	this.doHit = function() {

		if(game.currentPlayer().state === 'die') {
			return false;
		}

		this.state = 'hit';

		if(this.sprite.animationDestroyed === true) {
			this.hitDate = +new Date() + this.conf.hitSpeed;
			game.currentPlayer().strike();
			this.state = 'idle';
		}

		return true;
	}

	this.doDie = function() {
		game.mScore.updateScore(this.conf.score);
		this.toGarbage = true;
	}
// --- END ACTIONS
	
// UTILS ---
	this.debug = function() {
		CTX.fillStyle = "rgba(255,0,0,.3)";
		CTX.fillRect(this.x, this.y, this.w, this.h);
	}
// --- END UTILS

};